package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectInfoDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.PageInfo;
import com.jingdianjichi.subject.common.entity.PageResponse;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * SubjectInfoController 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/20 16:25
 * @description 题目Controller
 */
@RestController
@Slf4j
@RequestMapping("/subject/info")
public class SubjectInfoController {
    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 增加题目信息
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectInfoController add dto:{}", JSON.toJSONString(subjectInfoDTO));
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryIds(), "题目分类不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelIds(), "题目标签不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBo(subjectInfoDTO);
            List<SubjectOptionBO> subjectOptionBOList = SubjectAnswerDTOConverter.INSTANCE.convertDTOToBo(subjectInfoDTO.getSubjectOptionList());
            subjectInfoBO.setSubjectOptionList(subjectOptionBOList);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectInfoController add error:{}", e.getMessage(), e);
            return Result.fail(false);

        }
    }
    @PostMapping("/queryByPage")
    public Result<PageResponse<SubjectInfoBO>> queryByPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectInfoController queryByPage dto:{}", JSON.toJSONString(subjectInfoDTO));
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "所选分类不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "所选标签不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBo(subjectInfoDTO);
            PageResponse<SubjectInfoBO> subjectInfoBOPageResponse = subjectInfoDomainService.queryCountByCondition(subjectInfoBO);
            return Result.ok(subjectInfoBOPageResponse);
        } catch (Exception e) {
            log.error("SubjectInfoController queryByPage error:{}", e.getMessage(), e);
            return Result.fail(null);

        }
    }
    @PostMapping("/querySubjectDetail")
    public Result<SubjectInfoBO> querySubjectDetail(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {

            if (log.isInfoEnabled())
                log.info("SubjectInfoController querySubjectDetail dto:{}", JSON.toJSONString(subjectInfoDTO));
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDTOToBo(subjectInfoDTO);
            SubjectInfoBO subjectInfoBO1 = subjectInfoDomainService.querySubjectDetail(subjectInfoBO);
            return Result.ok(subjectInfoBO1);
        } catch (Exception e) {
            log.error("SubjectInfoController querySubjectDetail error:{}", e.getMessage(), e);
            return Result.fail(null);

        }
    }
}
